#pragma once
#include "stdafx.h"
#include "UIFactory.h"
#include "DrawDialog.h"
#include "ResourceHelper.h"
namespace UILib
{
    class MessageWnd : public CWindowImpl<MessageWnd, CWindow, CWinTraits<0,0> >
        , public MessageDispatch
    {
    public:
        MessageWnd() {

        };
        ~MessageWnd(void){};

        DECLARE_WND_CLASS(_T("UiLib_MessageWnd"))
        BEGIN_MSG_MAP(MessageWnd)
            MESSAGE_HANDLER(WM_CREATE, OnCreate)
            MESSAGE_HANDLER(WM_CLOSE, OnClose)
            MESSAGE_HANDLER(WM_TIMER, OnTimer)
        END_MSG_MAP()

        virtual void AddObserver( RanderObserver* pObserver ){
            vObserver_.push_back(pObserver);
        };
        virtual void RemoveObserver( RanderObserver* pObserver ){
            for ( vector< RanderObserver* >::iterator it = vObserver_.begin(); it != vObserver_.end(); it++ )
            {
                if ( (*it) == pObserver )
                {
                    vObserver_.erase(it);
                    break;
                }
            }
        };
    protected:
        virtual LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){
            SetTimer(WM_USER + 0x7FF, 40);
            return S_OK;
        };
        virtual LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){
            KillTimer(WM_USER + 0x7FF);
            return S_OK;
        };
        virtual LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled){
            if ( WM_USER + 0x7FF ==  (UINT)wParam )
            {
                for ( vector< RanderObserver* >::iterator it = vObserver_.begin(); it != vObserver_.end(); it++ )
                {
                    RanderObserver* pObserver = (*it);
                    if ( pObserver )
                    {
                        pObserver->OnLayoutCheck();
                    }
                }
            }
            return S_OK;
        };
    private:
        vector< RanderObserver* > vObserver_;
    };

    CUIFactory::CUIFactory( void ) : pMsgWnd_(NULL)
    {

    }

    CUIFactory::~CUIFactory()
    {

    }

    CUIFactory* CUIFactory::Instance()
    {
        static CUIFactory sigleton;
        return &sigleton;
    }

    void CUIFactory::InitEngine( const wstring wszResDLL, RenderType eType )
    {
        CResourceHelper::GetInstance()->Init(wszResDLL);

        MessageWnd* pMsgWnd = new MessageWnd();
        pMsgWnd->Create(HWND_MESSAGE);

        pMsgWnd_ = dynamic_cast< void* >(pMsgWnd);
        int j = 0;
    }

    void CUIFactory::unInit()
    {
        CResourceHelper::GetInstance()->unInit();
    }

    IUIDialog* CUIFactory::Get_Dialog()
    {
        CDrawDialog* pDlg = new CDrawDialog();
        return (IUIDialog*)pDlg;
    }

    MessageDispatch* CUIFactory::GetMessageDispatch()
    {
        return dynamic_cast<MessageDispatch*>((MessageWnd*)pMsgWnd_);
    }
}
